package fr.utbm.lo54.project.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.utbm.lo54.project.fr.unitaire.JDBCOptionDAO;
import fr.utbm.lo54.project.module.Option;

/**
 * Servlet implementation class RegSuccessServlet
 */
public class RegSuccessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegSuccessServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] checks = request.getParameterValues("check_option");
		JDBCOptionDAO jdbcOption = new JDBCOptionDAO();
		Set<Option> listCourses = null;
		int[] sessionIds = new int[checks.length];
//		HttpSession session = request.getSession();
//		int clientId = Integer.parseInt((String) session.getAttribute("client_id"));
		
		for(int i = 0; i < checks.length; i++){
//			jdbcOption.addSessionToClient(clientId, Integer.parseInt(str));
			sessionIds[i] = Integer.parseInt(checks[i]);					
		}
		listCourses = jdbcOption.getListCoursesRegistered(sessionIds);
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		
		out.println("<h2>Congratulations! You have just registered in these courses </h2>");
		out.println("If you want to manager your schedules, you can access your admin page by clicking here: ");
		out.println("<a href=\"http://localhost:8080/PROJET_LO54/HomeServlet\">Manage Page</a>");
	
		out.println("<form name=\"myCourses\" method=\"GET\" action=\"HomeServlet\">");
		out.println("<table col=\"4\"><tr>");
		out.println("<th>Code</th><th>Title</th><th>Sessions</th><th>Location</th><th></th></tr>");
		
		for(Option o : listCourses) {
			out.println("<tr>");
			out.println("<td>" + o.getCode() + "</td>");
			out.println("<td>" + o.getTitle() + "</td>");
			out.println("<td>" + o.getDate_start() + " to " + o.getDatae_end() + "</td>");
			out.println("<td>" + o.getLocation() + "</td>");
			out.println("<td></td></tr>");
		}

		out.println("</table>");

		out.println("<input type=\"submit\" value=\"Return to home page\"/>");
		out.println("</form>");

		out.println("</body>");
		out.println("</html>");
		
	}

}
